package com.cloud.sysmana.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.cloud.sysmana.dto.FuncTree;
import com.cloud.sysmana.entity.SysFunc;
import com.cloud.sysmana.entity.SysUserRole;
import com.cloud.sysmana.ser.impl.FuncRoleCacheService;
import com.cloud.util.base.UserInfoCache;
import com.cloud.util.log.ALogFactory;

/**
 * 
 * 用户操作界面操作菜单
 * 
 * @version 1.0
 * @date 2012-09-01
 * @author DWZ框架组
 */
@SuppressWarnings("serial")
public class FuncMenuTag extends TagSupport {

	/** 查询用户能够使用的功能菜单信息 */
	public int doStartTag() throws JspException {

		ApplicationContext ac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(pageContext
						.getServletContext());// spring容器
		FuncRoleCacheService funcRoleCache = (FuncRoleCacheService) ac
				.getBean("funcRoleCache");// 功能、岗位缓存信息

		UserInfoCache useInfo = (UserInfoCache) pageContext.getSession()
				.getAttribute(UserInfoCache.USER_INFO_SESSION_CACHE_KEY);// 用户信息

		JSONArray jsonTree = null;
		FuncTree funcTree = new FuncTree();
		SysFunc func = new SysFunc();
		func.setFuncId(0);//根目录为0
		func.setFuncName("功能结构列表Root");
		funcTree.setFunc(func);// 功能树根		

		if (null != useInfo && null != useInfo.getUserInfo()) {
			List<SysFunc> userFuncList = null;// 用户的权限信息
			List<SysFunc> roleFuncList = new ArrayList<SysFunc>();// 指定岗位权限信息

			List<SysUserRole> userRoles = useInfo.getUserRoles();// 用户可存在多岗位
			userFuncList = funcRoleCache.getPubFucList();// 公共访问权限
			
			/*用户有岗位，则获取岗位权限*/
			if(userRoles != null && !userRoles.isEmpty()){
				boolean haveFlag = false;
				for (SysUserRole role : userRoles) {
					
					int roleId = role.getId().getRoleId();//获取岗位id
					if(roleId == 0){
						continue;
					}
					
					roleFuncList = funcRoleCache.getRoleFuncByRoleId(roleId);// 获取指定岗位权限
					for (SysFunc func1 : roleFuncList) {
						if(func1 == null){
							continue;
						}
						haveFlag = false;
						for (SysFunc func2 : userFuncList) {
	 						if (func2.getFuncUrl().equals(func1.getFuncUrl())) {
								haveFlag = true;
								break;
							}
						}
						if (!haveFlag) {
							userFuncList.add(func1); //将公共功能与该岗位下的权限功能拼起来
						}
					}
				}
			}
			
			//Collections.sort(userFuncList);// 排序
		
			funcTree = funcRoleCache.getFuncTree(funcTree, userFuncList);
			try {
				jsonTree = FuncTree.printFuncMenu(funcTree, new JSONArray());// 构建符合页面的菜单树
			} catch (JSONException e) {
				e.printStackTrace();
			}
		} else {
			ALogFactory.getLog().info(
					"操作者[" + pageContext.getRequest().getRemoteHost() + "]未登录");// 记日志
		}
		
		try {
			pageContext.getOut().println(jsonTree.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return super.doStartTag();

	}

}
